import moment from 'moment'
// 取剩余秒
const pluralize = (time, label) => {
    return time + label + '前'
}

// 相对时间过滤器，传入时间，返回距离今天有多久
export const timeAgo = time => {
    time = time instanceof Date ? time : new Date(time)
    const between = Date.now() / 1000 - (Number(time) / 1000)
    if (between < 3600) {
        if (Object.is(~~(between / 60), 0)) return '刚刚'
        return pluralize(~~(between / 60), ' 分钟')
    } else if (between < 86400) {
        return pluralize(~~(between / 3600), ' 小时')
    } else {
        return pluralize(~~(between / 86400), ' 天')
    }
}

// 转换为本地时间格式
export const toLocalString = date => {
    return date ? new Date(date).toLocaleString() : date
}

// YMDHMS时间转换过滤器
export const toYMD = date => {
    if (!date) return date
    date = new Date(date)
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours() > 11 ? '下午' : '上午'}`
}
// YMDHMS时间转换过滤器
export const format = date => {
    return moment(date).format('YYYY-MM-DD')
    // if (!date) return date
    // date = date.replace(/-/g, '/')
    // date = new Date(date)
    // return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()<10?'0'+date.getHours():date.getHours()}:${date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()}:${date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()}`
}
// hhmmss时间转换过滤器
export const hhmmss = date => {
    if (!date) return date
    date = new Date(date)
    return ` ${date.getHours() < 10 ? '0' + date.getHours() : date.getHours()}:${date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()}:${date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()}`
}
export const yyyymmdd = date => {
    if (!date) return date
    date = new Date(date)
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
}
/**
 * 秒转换为小时
 * @param int  value 总秒数
 * @return string result 格式化后的字符串
 */
export const formatSecondsToHour = (value) => {
    console.log(value)
    var theTime = parseFloat(value); // 需要转换的时间秒 
    var theTime1 = 0; // 分 

    var theTime2 = 0; // 小时 
    if (theTime > 60) {
        theTime1 = parseFloat(theTime / 60);
        theTime = parseFloat(theTime % 60);
        if (theTime1 > 60) {
            theTime2 = parseFloat(theTime1 / 60);
        }
    }

    if (theTime2 > 0) {
        return parseFloat(theTime2).toFixed(2) + '小时';
    } else {

        return parseFloat(theTime1).toFixed(2) + '分钟';

    }

}

function padding(val) {
    if (parseInt(val) < 10) {
        return "0" + parseInt(val)
    }
    return parseInt(val);
}

/**
 * 格式化秒
 * @param int  value 总秒数
 * @return string result 格式化后的字符串
 */
export const formatSeconds = (value) => {
    var theTime = parseFloat(value); // 需要转换的时间秒 
    var minutes = 0; // 分 
    var hour = 0; // 小时 
    var day = 0; // 天
    if (theTime > 60) {
        minutes = parseFloat(theTime / 60);
        theTime = parseFloat(theTime % 60);
        if (minutes > 60) {
            hour = parseFloat(minutes / 60);
            minutes = parseFloat(minutes % 60);
            if (hour > 24) {
                //大于24小时
                day = parseFloat(hour / 24);
                hour = parseFloat(hour % 24);
            }
        }
    }
    var result = '';
    if (theTime > 0) {
        result = "" + padding(theTime);
    }
    if (minutes > 0) {
        result = "" + padding(minutes) + ":" + result;
    }
    if (hour > 0) {
        result = "" + padding(hour) + ":" + result;
    }
    if (day > 0) {
        result = "" + padding(day) + ":" + result;
    }
    return result;
}